package cn.com.api.controller;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_feedback;

import cn.com.api.service.ApiT_feedbackService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "feedback/")
public class ApiT_feedbackController {
	@Autowired
	private ApiT_feedbackService apiT_feedbackService;

	private static Logger logger = Logger
			.getLogger(ApiT_feedbackController.class);

	/**
	 * 新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertT_feedback")
	public void insertT_feedback(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String customer_id = request.getParameter("customer_id");
		String message = request.getParameter("message");
		/*
		 * String published = request.getParameter("published"); String answer =
		 * request.getParameter("answer"); String create_date =
		 * request.getParameter("create_date"); String answer_date =
		 * request.getParameter("answer_date"); String answer_id =
		 * request.getParameter("answer_id");
		 */
		/* message = new String(message.getBytes("ISO-8859-1"), "utf-8"); */

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (customer_id != null && !"".equals(customer_id.trim())) {
				ApiT_feedback t_feedback = new ApiT_feedback();

				t_feedback.setCustomer_id(customer_id);
				t_feedback.setMessage(message);
				// t_feedback.setPublished(published);
				Date d = new Date();
				// SimpleDateFormat sdf = new
				// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				// String dateNowStr = sdf.format(d);
				t_feedback.setCreate_date(d);

				apiT_feedbackService.insertT_feedback(t_feedback);
				// return
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_feedbackById")
	public void findT_feedbackById(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			ApiT_feedback t_feedback = apiT_feedbackService
					.findT_feedbackById(Integer.valueOf(id));

			// return
			map.put("datas", t_feedback);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateT_feedback")
	public void updateT_feedback(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String f_id = request.getParameter("f_id");

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (f_id != null && !"".equals(f_id.trim())) {
				ApiT_feedback t_feedback = new ApiT_feedback();
				// 获取原数据
				t_feedback = apiT_feedbackService.findT_feedbackById(Integer
						.valueOf(f_id));
				//
				// t_feedback.setIs_open(Integer.valueOf(is_open));
				//
				apiT_feedbackService.updateT_feedback(t_feedback);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteT_feedback")
	public void deleteT_feedback(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {

				Integer ret_I = apiT_feedbackService.deleteT_feedback(Integer
						.parseInt(id));
				if (ret_I > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "删除成功！");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "检查失败！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
